/*
 * CS_UPGRADE_STONE_GACHYA_MAIL.cpp
 *
 *  Created on: 2014. 3. 3.
 *      Author: mega
 */

#include "../../User.h"
#include "../../Command.h"
#include "../../NLogic/Item.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/MailBox.h"
#include "../../NUtil/Utils.h"
#include "../../NResource/Static.h"


namespace ProjectZ { namespace NCommand {

static void SendFailUpStoneGachyaMail(User * pUser, int ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_UPGRADE_STONE_GACHYA_MAIL);
	pUser->Send(pAck);
}

void Command::CS_UPGRADE_STONE_GACHYA_MAIL(User * pUser, ::Network::Packet * pPacket)
{
/*	CS_UPGRADE_STONE_GACHYA_MAIL	0x231C
	{
		u64	u64mail_seq;	// 발급된 메일Seq
	}

	SC_UPGRADE_STONE_GACHYA_MAIL	0x231D
	{
		s16		ack;

		s8		class_type;
		u8		sheet_type;
		u16		item_tid;

		u8		level;
		u8		item_quality;

		/// 강화석 효과는 이펙트 1번
		u8		eff_type1;
		u8		eff_pos1;
		u16		eff_value1;
	}*/

	PREPARE_PACKET;

	uint64_t mailSeq = 0;

	*pPacket >> mailSeq;

	NLogic::Bag * pBag = pUser->GetBag(BAG_TYPE_ACCESSORY);
	if (NULL == pBag)
	{
		DEBUGLOG("%s : pBag is NULL!", __FUNCTION__);
		SendFailUpStoneGachyaMail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NResource::BalanceData * pBalance = NResource::Static::InstancePtr()->GetBalance()->GetBalancePtr(pUser);
	if (NULL == pBalance)
	{
		DEBUGLOG("%s : pBalance is NULL", __FUNCTION__);
		SendFailUpStoneGachyaMail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NLogic::MailBox * pMailBox = pUser->GetMailBox();
	if (NULL == pMailBox)
	{
		DEBUGLOG("%s : pMailBox is NULL", __FUNCTION__);
		SendFailUpStoneGachyaMail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NLogic::MailBox::Mail * pMail = pMailBox->GetMailData(mailSeq);
	if (NULL == pMail)
	{
		DEBUGLOG("%s : pMail is NULL", __FUNCTION__);
		SendFailUpStoneGachyaMail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	// 아이템 첨부 상태 확인
	if (EM_GIFT_ITEM != (pMail->GetData().gift_status & EM_GIFT_ITEM))
	{
		DEBUGLOG("%s : Not Found Item in Mail", __FUNCTION__);
		SendFailUpStoneGachyaMail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NResource::UpStoneGachyaResource::UpStoneGachyaType eType;

	switch (pMail->GetData().item.eff_value[0])
	{
	case 0 :
		eType = NResource::UpStoneGachyaResource::UpStoneGachyaType::EM_FREE_GACHYA;
		break;

	case 1 :
		eType = NResource::UpStoneGachyaResource::UpStoneGachyaType::EM_CHARGE_GACHYA;
		break;

	default :
		DEBUGLOG("%s : INVALID GachyaType=%d", __FUNCTION__, pMail->GetData().item.eff_value[0]);
		SendFailUpStoneGachyaMail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}


	// 강화석 등급 결정
	int quality = 0;
	if (false == NResource::Static::InstancePtr()->GetUpStoneGachyaResource()->GetUpStoneGachyaQuality(eType, quality))
	{
		DEBUGLOG("%s : UpStone Quality Gachya Fail", __FUNCTION__);
		SendFailUpStoneGachyaMail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	int minLevel = pUser->GetCharacterInfo().level - pBalance->GetValue(EM_UPSTONE_GACHYA_LEVEL_VARIATION);
	if (minLevel <= 0)
	{
		minLevel = 1;
	}

	int maxLevel = pUser->GetCharacterInfo().level + pBalance->GetValue(EM_UPSTONE_GACHYA_LEVEL_VARIATION);
	int limitLevel = NResource::Static::InstancePtr()->GetCommon()->GetLimitLevel(pUser);
	if (maxLevel > limitLevel)
	{
		maxLevel = limitLevel;
	}

	// 강화석 데이터 생성
	NLogic::Item::Data clsData;
	clsData.quantity	= 1;
	clsData.class_type	= -1;
	clsData.tid			= UPSTONE_TID;
	clsData.sub_type	= CLASS_ITEM_TABLE_ETC;
	clsData.bag_type 	= BAG_TYPE_ACCESSORY;

	clsData.level		= NUtil::GetRandBetween(minLevel, maxLevel);
	clsData.quality		= quality;

	// 강화석 데이터 저장
	bool bBagFull = false;
	NLogic::Item * pItem = pBag->AddItemWithData(clsData, bBagFull);
	if (NULL == pItem)
	{
		if (true == bBagFull)
		{
			SendFailUpStoneGachyaMail(pUser, ACK_INVEN_BAG_FULL);
			return;
		}

		DEBUGLOG("%s : pItem is NULL!!", __FUNCTION__);
		return;
	}


	// 응답
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	*pAck << static_cast<int8_t>(pItem->GetData().class_type);
	*pAck << static_cast<int8_t>(pItem->GetData().sub_type);
	*pAck << static_cast<uint16_t>(pItem->GetData().tid);
	*pAck << static_cast<uint8_t>(pItem->GetData().level);
	*pAck << static_cast<uint8_t>(pItem->GetData().quality);
	*pAck << static_cast<uint8_t>(pItem->GetData().eff_type[1]);
	*pAck << static_cast<uint8_t>(pItem->GetData().eff_pos[1]);
	*pAck << static_cast<int16_t>(pItem->GetData().eff_value[1]);
	pAck->MakePacket(CMD_SC_UPGRADE_STONE_GACHYA_MAIL);
	pUser->Send(pAck);

	// 메일의 첨부 상태 갱신
	pMail->GetData().gift_status = pMail->GetData().gift_status ^ EM_GIFT_ITEM;
	pMail->UpdateGiftStatusDatabaseMail();
}


} /*NCommand*/ } /*ProjectZ*/
